<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" id="demo">
    <div id="tips"></div>
</body>
<script>
    // try/catch/finally 语句用于处理代码中可能出现的错误信息。(异常捕获机制)

    /*    var a = 1;
       var b = 2;
       var c = aa + b;
       console.log(c);
       console.log("代码执行完毕"); */

    /*     try {
            var a = 1;
            var b = 2;
            var c = aa + b;
            console.log(c);
        } catch (err) {
            console.log(err);
        } finally {
            console.log("代码执行完毕");
        }
    
        console.log("try...catch....finally"); */



    // try ...catch ...finally  只能捕获代码在执行过程同步问题
    /* try {
        setTimeout(function () {
            var a = 1;
            var b = 2;
            var c = aa + b;
            console.log(c);  // 异步在未来抛错 ,此时 try ...catch ...finally已经执行完毕 => 没有办法捕获  => 解决方法  异步任务中嵌套try catch

        }, 1000)
    } catch (err) {
        console.log(err);
    } finally {
        console.log("代码执行完毕");
    }

    console.log("try...catch....finally"); */


    // 浏览器兼容
    // function getStyle(ele, attr) {
    //     if (window.getComputedStyle) {
    //         return window.getComputedStyle(ele)[attr];
    //     } else {
    //         return ele.currentStyle[attr];
    //     }
    // }

    // function getStyle(ele, attr) {
    //     try {
    //         return window.getComputedStyle(ele)[attr];
    //     } catch (err) {
    //         return ele.currentStyle[attr];
    //     }
    // }


    var demo = document.getElementById("demo");
    var tips = document.getElementById("tips");

    demo.oninput = function(){
        var val = demo.value.trim();

        try{
            if(isNaN(val)){
                throw "请输入数字";
            }else if(val < 0){
                throw "请输入大于0的数字";
            }else if(val > 100){
                throw "请输入小于100的数字";
            }else{
                throw val;
            }
        }catch(err){
            tips.textContent = err;
        }
    }
</script>
</html>